package com.example.myapplication.status2;


import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class StatusDownloading extends Status {
    public StatusDownloading(StatusContext statusContext) {
        super(statusContext);
    }

    @Override
    public void doAction() {

        FileUtils.getSingleExcutor().execute(() -> {
            new Timer().schedule(new TimerTask() {
                @Override
                public void run() {
                    boolean downLoadSuccess = new Random().nextBoolean();
                    if (downLoadSuccess) {
                        statusContext.setStatus(new StatusInstall(statusContext));
                        statusContext.changeEvent(null);
                    } else {
                        statusContext.setStatus(new StatusDownloadFailed(statusContext));
                    }
                }
            }, 2000);
        });
    }

    @Override
    public void translateStatus() {
        System.out.println("status - 正在下载");
        statusContext.updateView(Event.DOWNLOADING);
    }

    @Override
    public void eventChange(Event event) {
        doAction();
    }

}
